import { getOffsetTop } from 'element-plus/lib/utils';
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { CheckLoginStatus, getCookie, setCookie, ClearCookie} from '../utils/cookie'
import { HomeFilled } from '@element-plus/icons';
// import LoginView from '../views/LoginView.vue'


export const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'app',
    redirect: '/login',
    meta: {
      title: '登录',
      icon: 'HomeFilled'
    },
    component: () => import('../App.vue')
  },
  /**
   * 登录
   */
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录',
      icon: 'HomeFilled'
    },
    component: () => import('../views/LoginView.vue')
  },
  /**
   * 咨询师
   */
  {
    path: '/consultant',
    name: 'consultant',
    redirect: '/home',
    component: () => import('../views/consultant/ConsultantView.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          title: '首页',
          icon: 'icon-home'
        },
        component: () => import('../views/consultant/component/homeIndex.vue')
      },
      {
        path: '/history',
        name: 'history',
        meta: {
          title: '咨询记录',
          icon: 'icon-history'
        },
        component: () => import('../views/consultant/component/historyIndex.vue')
      },
      {
        path: '/info',
        name: 'info',
        meta: {
          title: '个人信息',
          icon: 'icon-info'
        },
        component: () => import('../views/consultant/component/info.vue')
      },
      {
        path: '/bind',
        name: 'bind',
        meta: {
          title: '绑定督导',
        },
        component: () => import('../views/consultant/component/bind.vue')
      }



    ]
  },
  /**
   * 督导
   */
  {
    path: '/supervisor',
    name: 'supervisor',
    redirect: '/home2',
    component: () => import('../views/supervisor/SupervisorView.vue'),
    children: [
      {
        path: '/home2',
        name: 'home2',
        meta: {
          title: '首页',
          icon: 'icon-home'
        },
        component: () => import('../views/supervisor/component/homeIndex.vue')
      },
      {
        path: '/record',
        name: 'record',
        meta: {
          title: '咨询记录',
          icon: 'icon-record'
        },
        component: () => import('../views/supervisor/component/historyIndex.vue')
      },
      {
        path: '/info2',
        name: 'info2',
        meta: {
          title: '个人信息',
          icon: 'icon-info'
        },
        component: () => import('../views/supervisor/component/info.vue')
      },

    ]
  },
  /**
   * 机构管理员
   */
  {
    path: "/administrator",
    name: "administrator",
    redirect: '/homePage',
    meta: {
      title: '管理界面',
      icon: 'icon-home'
    },
    component: () => import("../views/admin/AdministratorView.vue"),
    children: [
      {
        path: "/homePage",
        name: "homePage",
        component: () =>
          import("../views/admin/component/homePage.vue"),
      },
      {
        path: "/recordPage",
        name: "recordPage",
        component: () =>
          import("../views/admin/component/recordPage.vue"),
      },
      {
        path: "/scheduleTable",
        name: "scheduleTable",
        component: () =>
          import("../views/admin/component/scheduleTable.vue"),
      },
      {
        path: "/consultantManagement",
        name: "consultantManagement",
        component: () =>
          import(
            "../views/admin/component/consultantManagement.vue"
          ),
      },
      {
        path: "/supervisorManagement",
        name: "supervisorManagement",
        component: () =>
          import(
            "../views/admin/component/supervisorManagement.vue"
          ),
      },
      {
        path: "/userManagement",
        name: "userManagement",
        component: () =>
          import("../views/admin/component/userManagement.vue"),
      },
    ]
  },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});


router.beforeEach((to,from,next) =>{
  
    if(to.path != '/login'){
      console.log('not login')
    if(CheckLoginStatus()){
        next()
      } else {
        ClearCookie()
        alert('无权限')
        next('/login')
      }

    } else {
      if(CheckLoginStatus()){
        const auth = getCookie('auth')
        next(auth)
      } else {
        ClearCookie()
        next()
      }
    }
   
})

export default router
